package priv.pront.code.national.acwing.dp.bags;

import java.util.Scanner;

public class Acwing10_有依赖的背包问题 {

    static int n, V;
    static int[] v, w, p, nv, nw;
    static int[] dp;
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt(); V = scanner.nextInt();
        v = new int[n + 1];
        w = new int[n + 1];
        p = new int[n + 1];
        nv = new int[n + 1];
        nw = new int[n + 1];
        dp = new int[V + 1];
        for(int i = 1; i <= n; i++){
            v[i] = scanner.nextInt();
            w[i] = scanner.nextInt();
            p[i] = scanner.nextInt();
        }
        preHand();
        for(int i = 1; i <= n; i++){
            for(int j = V; j >= nv[i]; j--){
                dp[j] = Math.max(dp[j], dp[j - nv[i]] + nw[i]);
            }
        }
        System.out.println(dp[V]);
    }

    private static void preHand(){
        for(int i = 1; i <= n; i++){
            int cur = p[i];
            nv[i] = v[i];
            nw[i] = w[i];
            while(cur != -1){
                nv[i] += v[cur];
                nw[i] += w[cur];
                cur = p[cur];
            }
        }
    }
}
